package cn.usian.service.impl;

import cn.usian.common.R;
import cn.usian.mapper.GoodsMapper;
import cn.usian.pojo.Goods;
import cn.usian.service.GoodsService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

@Service
public class GoodsServiceImpl implements GoodsService {
    @Resource
    private GoodsMapper goodsMapper;


    @Override
    public Page<Goods> findPage(Integer page, Integer pageSize, String name) {
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        Page<Goods> page1 = new Page<>(page, pageSize);
        queryWrapper.like(name != null, "name", name);
        Page<Goods> page2 = goodsMapper.selectPage(page1, queryWrapper);
        return page2;
    }

    @Override
    public R add(Goods goods) {
        goodsMapper.insert(goods);
        return R.success("添加成功");
    }

    @Override
    public R update(Goods goods) {
        goodsMapper.updateById(goods);
        return R.success("修改成功");
    }

    @Override
    public R findById(Integer id) {
        Goods goods = goodsMapper.selectById(id);
        return R.success(goods);
    }

    //自动审核
    @Override
    public void selectStatus2() {
        List<Goods> goodsList = goodsMapper.selectList(null);
        for (Goods goods : goodsList) {
            Integer count = goodsMapper.selectName(goods.getName());
            Integer count2 = goodsMapper.selectName2(goods.getName());
            Integer id = goodsMapper.selectName3(goods.getName());
            if (goods.getStatus2() == 1 && goods.getNum() <= 100 && count < 2) {
                goods.setStatus3(1);
                goodsMapper.updateById(goods);

                if (count == 1) {
                    goodsMapper.edit(goods.getName(), id);
                }else {
                    goods.setNum3(0);
                    goods.setNum2(0);
                    goodsMapper.updateById(goods);
                }

            } else {
                if (count==0){
                    goods.setNum3(0);
                }
                goods.setNum2(1);
                goods.setStatus2(0);
                goods.setStatus3(0);
                goods.setStatus4(0);
                goodsMapper.updateById(goods);

                if (goods.getNum() < 100) {
                    goods.setNum2(0);
                    goodsMapper.updateById(goods);
                }
            }
        }
    }

    @Override
    public R delete(String ids) {
        //判断是否启用
        Integer count = goodsMapper.selectStatus1(ids);
        if (count != 0) {
            return R.error("该商品已启用，无法直接删除！");
        }
        //创建对象
        Goods goods = new Goods();
        //先切分
        String[] split = ids.split(",");
        //将数组转换成集合
        List<String> strings = Arrays.asList(split);
        goodsMapper.deleteBatchIds(strings);
        //返回结果
        return R.success("删除成功！");
    }



    //批量启动-禁用
    @Override
    public R editStatus(String ids, Integer status) {
        Integer count = goodsMapper.selectStatus2(ids);
        if (count != 0) {
            return R.error("该商品正在审核中，无法直接禁用！");
        }
        //创建对象
        Goods goods = new Goods();
        //先切分
        String[] split = ids.split(",");
        //遍历切分后的数组
        for (String s : split) {
            goods.setId(Integer.valueOf(s));
            goods.setStatus1(status);
            goodsMapper.updateById(goods);
        }
        return R.success("修改成功");
    }

    @Override
    public R updateStatus2(String ids, Integer status) {
        //创建对象
        Goods goods = new Goods();
        //先切分
        String[] split = ids.split(",");
        //遍历切分后的数组
        for (String s : split) {
            goods.setId(Integer.valueOf(s));
            goods.setStatus2(status);
            goodsMapper.updateById(goods);
        }
        return R.success("修改成功");
    }

    @Override
    public R updateStatus3(String ids, Integer status) {
        //创建对象
        Goods goods = new Goods();
        //先切分
        String[] split = ids.split(",");
        //判断是否通过审核
        Integer count = goodsMapper.selectStatus3(ids);
        if (count != 0) {
            return R.error("该商品审核未通过，无法上架！");
        }

        //遍历切分后的数组
        for (String s : split) {
            goods.setId(Integer.valueOf(s));
            goods.setStatus4(status);
            goods.setNum2(0);
            goods.setNum3(0);
            goodsMapper.updateById(goods);
        }
        return R.success("修改成功");
    }

}
